<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

class f_mail_MailMime
{
	private $mailMessage;
	private $frontier;

	private function __construct($mailMessage)
	{
		$this->mailMessage = $mailMessage;
		$this->frontier = md5(uniqid(mt_rand()));
	}

	/**
	 * Enter description here...
	 *
	 * @param f_mail_MailMessage $mailMessage
	 * @return f_mail_MailMime
	 */
	public static function getInstance($mailMessage)
	{
		return new self($mailMessage);
	}

	public function getHTML()
	{
		$mail = $this->getHTMLMessage();
		$mail .= $this->addAttachment();
		$mail .= '--'.$this->getFrontier().'--'."\n";
		return $mail;
	}	
	
	public function getText()
	{
		$mail = $this->getTextMessage();
		$mail .= $this->addAttachment();
		$mail .= '--'.$this->getFrontier().'--'."\n";
		return $mail;
	}

	private function getFrontier()
	{
		return $this->frontier;
	}

	private function getMailMessage()
	{
		return $this->mailMessage;
	}

	public function getHeaders($smtp = false)
	{
		$headers  = 'From: '.$this->getMailMessage()->getSenderName().' <'.$this->getMailMessage()->getSender().'>'."\n";
		if($smtp)
		{
			$headers .= 'To: '.$this->getMailMessage()->getReceiverName().' <'.$this->getMailMessage()->getReceiver().'>'."\n";
			$headers .= 'Subject: '.$this->getMailMessage()->getSubject()."\n";
			$headers .= 'X-Priority: '.$this->getMailMessage()->getPriority()."\n";
			$headers .= 'X-Mailer: '.$this->getMailMessage()->getAppName()."\n";
		}
		$headers .= 'Return-Path: <'.$this->getMailMessage()->getReplyTo().'>'."\n";
		$headers .= 'MIME-Version: 1.0'."\n";
		$headers .= 'Content-Type: multipart/mixed; boundary="'.$this->getFrontier().'"'."\r\n\n";

		return $headers;
	}

	private function getHTMLMessage()
	{
		$message = '--'.$this->getFrontier()."\n";

		$message .= 'Content-Type: text/html; charset="'.$this->getMailMessage()->getEncoding().'"'."\n";
		$message .= 'Content-Transfer-Encoding: 8bit'."\n\n";

		$message .= $this->getMailMessage()->getHtmlContent()."\n\n";

		return $message;
	}

	private function getTextMessage()
	{
		$message = '--'.$this->getFrontier()."\n";

		$message .= 'Content-Type: text/plain; charset="'.$this->getMailMessage()->getEncoding().'"'."\n";
		$message .= 'Content-Transfer-Encoding: 8bit'."\n\n";

		$message .= $this->getMailMessage()->getTextContent()."\n\n";

		return $message;
	}

	private function addAttachment()
	{
		$attachment = $this->getMailMessage()->getAttachment();
		
		if(!is_null(end($attachment)))
		{
			$message = "";
			foreach ($this->getMailMessage()->getAttachment() as $attachment)
			{
				$file = f_file_File::getInstance($attachment);

				$message = '--'.$this->getFrontier()."\n";

				$message .= 'Content-Type: '.$file->getMimeType().'; name="'.$file->getBaseName().'"'."\n";
				$message .= 'Content-Transfer-Encoding: base64'."\n";
				$message .= 'Content-Disposition:attachement; filename="'.$file->getBaseName().'"'."\n\n";

				$message .= chunk_split(base64_encode(file_get_contents($file->getFile())))."\n";
			}

			return $message;
		}
	}
}